package com.back.campus_back.service.impl;

import com.back.campus_back.domain.Goods;
import com.back.campus_back.domain.WantedGoods;
import com.back.campus_back.mapper.WantedGoodsMapper;
import com.back.campus_back.service.WantedGoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author ${author}
 * @since 2021-04-17
 */
@Service
public class WantedGoodsServiceImpl extends ServiceImpl<WantedGoodsMapper, WantedGoods> implements WantedGoodsService {

    @Autowired
    WantedGoodsMapper wantedGoodsMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /*
     * 查询全部求购
     * */
    @Override
    public List<WantedGoods> AllWantsGoods(){
        List<WantedGoods> Goodslist = wantedGoodsMapper.selectList(null);
        return Goodslist;
    }

    /*
     * 查询全部求购：分页
     * */
    @Override
    public IPage<WantedGoods> AllWantsGoods(Integer current, Integer size){
        Page<WantedGoods> page = new Page<WantedGoods>(current,size);
        IPage<WantedGoods> goodsIPage = wantedGoodsMapper.selectPage(page, null);
        return goodsIPage;
    }

    /*
     * 根据用户的id查询用户个人发布的商品信息
     * */
    @Override
    public IPage<WantedGoods> findWantedGoodsByStuId(Integer student_id, Integer current, Integer size){
        QueryWrapper<WantedGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id",student_id);

        //返回分页数据
        Page<WantedGoods> page = new Page<WantedGoods>(current,size);
        IPage<WantedGoods> goodsIPage = wantedGoodsMapper.selectPage(page, wrapper);

        return goodsIPage;
    }


    /*
     * 发布求购
     * */
    @Override
    public String insertWantedGoods(WantedGoods wantedGoods){
        int result = wantedGoodsMapper.insert(wantedGoods);
        if(result == 1){
            return "add_success";
        }else{
            return "add_error";
        }
    }

    /*
     * 用户更新求购信息
     * */
    @Override
    public String updateWantedGoods(WantedGoods wantedGoods){
        try {
            int result = wantedGoodsMapper.updateById(wantedGoods);
            if(result == 1){
                return "up_success";
            }else{
                return "up_error";
            }
        }catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }
        return "up_error";

    }

    /*
     * 删除求购信息
     * */
    @Override
    public String deleteWantedGoodsById(Integer want_id){
        int result = wantedGoodsMapper.deleteById(want_id);
        if(result == 1){
            return "de_success";
        }else{
            return "de_error";
        }
    }


}
